package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.dto.FileListBean;
import com.xh.bussiness.system.bean.request.FileListRequest;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.bussiness.system.dao.FileListMapper;
import com.xh.bussiness.system.model.FileList;
import com.xh.bussiness.system.service.IFileListService;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2018-12-18
 */
@Service
public class FileListServiceImpl extends BaseServiceImpl<FileListMapper, FileList> implements IFileListService {

    @Override
    public FileListBean queryFileList(String md5) {
        QueryWrapper qw=new QueryWrapper<FileList>();
        //拼装条件
        qw.eq("file_md5",md5);
        return this.selectOne(qw,FileListBean.class);
    }


    @Override
    public XhPage queryFileListPage(XhPage page, FileListRequest bean) {
        QueryWrapper qw=new QueryWrapper<FileList>();
        //拼装条件
        qw=prepareParams(bean, qw);
        return (XhPage) this.selectPage(page,qw,FileListBean.class);
    }

    @Override
    public List queryFileList(FileListRequest bean) {
        QueryWrapper qw=new QueryWrapper<FileList>();
        //拼装条件
        qw=prepareParams(bean, qw);
        return this.selectList(qw, FileListBean.class);
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(FileListRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!StringUtils.isEmpty(bean.getFileMd5())){
            qw.eq("file_md5", bean.getFileMd5().trim());
        }
        if(!StringUtils.isEmpty(bean.getExt())){
            qw.eq("ext",bean.getExt().trim());
        }
        if (!StringUtils.isEmpty(bean.getFileName())) {
            qw.like("ori_filename", bean.getFileName());
        }
        return qw;
    }
}
